//
//  TabRenameView.m
//  Penda
//
//  Created by Duckjin Kang on 1/6/10.
//  Copyright 2010 StonyBrookUniv.. All rights reserved.
//

#import "TabRenameController.h"
#import "NSFileInfoItem.h"

@implementation TabRenameController

@synthesize prefix, suffix, startingNumber, numberPads, embedOriginalName, preserveExt, steps;
@synthesize trimLeft, trimRight;

- (void) awakeFromNib {
	self.suffix = [NSString stringWithString:@"-"];
	self.prefix = [NSString stringWithString:@"[Prefix]"];
	self.embedOriginalName = YES;
	self.numberPads = 4;
	self.startingNumber = 1;
	self.steps = 1;
	self.preserveExt = YES;
	[self addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"prefix" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"suffix" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"startingNumber" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"numberPads" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"embedOriginalName" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"preserveExt" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"steps" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"trimLeft" options:NSKeyValueObservingOptionNew context:nil];
	[self addObserver:self forKeyPath:@"trimRight" options:NSKeyValueObservingOptionNew context:nil];
}

- (void) refreshRenameTable {
	// re-evaluate names
	
	char *buf = "";
	if (numberPads) {
		buf = malloc(numberPads+1);
		memset(buf, '0', numberPads);
		buf[numberPads] = '\0';
	}
	NSMutableString *pNumber = [NSMutableString stringWithCString:buf encoding:NSASCIIStringEncoding];
	if (numberPads)
		free (buf);
	for (int i=0;i<[[self arrangedObjects] count];i++) {
		NSString *curNumber = [NSString stringWithFormat:@"%d",startingNumber+(i*steps)];
		if ([curNumber length] > numberPads) {
			[pNumber setString:curNumber];
		} else {
			NSRange range = {numberPads-[curNumber length],[curNumber length]};
			[pNumber replaceCharactersInRange:range withString:curNumber];
		}
		NSMutableString *pRet = [[NSMutableString alloc] init];
		NSFileInfoItem* pItem = (NSFileInfoItem*) [[self arrangedObjects] objectAtIndex:i];
		NSMutableString* title = [NSMutableString stringWithString:[pItem fileTitle]];
		[title deleteCharactersInRange:(NSRange){0,trimLeft}];
		[title deleteCharactersInRange:(NSRange){[title length]-trimRight,trimRight}];
		[pRet appendFormat:@"%@%@%@%@%@%@",
		 [prefix length]==0 ? @"" : prefix,
		 embedOriginalName ? title: @"",
		 [suffix length]==0 ? @"" : suffix,
		 pNumber,
		 (preserveExt && [[pItem fileExt] length]) ? @"." : @"",
		 (preserveExt && [[pItem fileExt] length]) ? [pItem fileExt] : @""
		 ];
		[pItem setNewName:pRet];
	}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	[NSObject cancelPreviousPerformRequestsWithTarget:self];
	[self performSelector:@selector(refreshRenameTable) withObject:nil afterDelay:0.05];
}

@end
